package com.xwli.webservice.cxf.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * @author xwli
 * @date: 2021/10/18 15:36
 * @desc
 * @since JDK 1.8
 */
public class CxfWebServiceClient {

    public static void main(String[] args) {
        String request = "小李";
        String response = callCxfWebService(request);
        System.out.println("接口响应信息:\n"+response);
    }

    private static String callCxfWebService(String request){
        // 创建动态客户端
        JaxWsDynamicClientFactory cxf = JaxWsDynamicClientFactory.newInstance();
        String url = "http://localhost:8002/ws/cxfWebServiceDemo?wsdl";
       // String url = "http://localhost:8112/ws/SB_JX_BPXT_ImportNewConstructionResultInfoSrv?wsdl";
        Client cxfClient = cxf.createClient(url);

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = cxfClient.invoke("cxfHello", request);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return objects[0].toString();
    }
}

